package calorie.com.calorie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import calorie.com.calorie.R;
import calorie.com.calorie.adapter.MallWaitSendAdapter;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.GoodsOrdersContact;
import calorie.com.calorie.eventbus.EventImpl;
import calorie.com.calorie.response.GoodsOrderResponce;
import retrofit2.Response;

//待发货
public class MallWaitSendActivity extends MvpActivity<GoodsOrdersContact.GoodsOrdersPresent> implements GoodsOrdersContact.IGoodsOrdersView {


    @BindView(R.id.tip)
    TextView tip;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    MallWaitSendAdapter mAdapter;

    List<GoodsOrderResponce.DataBean.OrderListBean> mList;

    int pageNo = 1;
    int status = 1;//待发货

    @Override
    public void initData(Bundle savedInstanceState) {
        updateMsg();
    }

    private void updateMsg(){
        mList = new ArrayList<>();
        mAdapter = new MallWaitSendAdapter(R.layout.fragment_mall_wait_send_item, mList);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        rv.setLayoutManager(manager);
        rv.setAdapter(mAdapter);
        int userId = SPUtils.getInt(Constant.USER_ID, -1, context);
        mPresent.goodsOrders(context, status, pageNo, userId);

        mAdapter.setOnItemChildClickListener(new MallWaitSendAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.order_wait_send_item:
                    case R.id.refund_btn:
                        Intent intent = new Intent(context, MallWaitSendDetailActivity.class);
                        intent.putExtra("goodsName", mList.get(position).getTitle());//商品名
                        intent.putExtra("goodsPrice", String.valueOf(mList.get(position).getAmount()));//金额
                        intent.putExtra("orderId", mList.get(position).getId());//单号
                        intent.putExtra("shippedName", mList.get(position).getReceivingName());
                        intent.putExtra("shippedPhone", mList.get(position).getReceivingPhone());
                        intent.putExtra("shippedAddress", mList.get(position).getReceivingAddress());

                        Date date1 = new Date(mList.get(position).getCreateTime());
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

                        intent.putExtra("goodsCreateTime", format.format(date1));
                        startActivity(intent);
                        break;
                }
            }
        });

        refresh.setOnRefreshListener(refreshLayout -> {
            pageNo = 1;
            mPresent.goodsOrders(context, status, pageNo, userId);
        });
        refresh.setOnLoadMoreListener(refreshLayout -> {
            pageNo++;
            mPresent.goodsOrders(context, status, pageNo, userId);
        });
    }
    @Override
    public boolean useEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveActivity(EventImpl.CancelOrderEvent event) {
        if (!ObjectUtils.isEmpty(event)) {
            updateMsg();
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_mall_wait_send;
    }

    @Override
    protected GoodsOrdersContact.GoodsOrdersPresent createPresent() {
        return new GoodsOrdersContact.GoodsOrdersPresent();
    }

    //待发货返回数据
    @Override
    public void getGoodsOrdersResult(Response<GoodsOrderResponce> response) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            List<GoodsOrderResponce.DataBean.OrderListBean> data = response.body().getData().getOrderList();
            if (!ObjectUtils.isEmpty(data)) {
                tip.setVisibility(View.GONE);
                mList.clear();
                mList.addAll(data);
                mAdapter.notifyDataSetChanged();
                rv.setVisibility(View.VISIBLE);
            } else {
                tip.setVisibility(View.VISIBLE);
                rv.setVisibility(View.GONE);
            }
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }

    }

}
